package zd.vrp.ga;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import zd.vrp.global.City;
import zd.vrp.global.GlobalInformation;
import zd.vrp.global.Route;
import zd.vrp.utils.MyComparator;
import zd.vrp.utils.SortObject;
import zd.vrp.view.PlotSolution1;

/**
 * generate initial solution for genetic algorithm
 * @author zd
 *
 */
public class InitialSolution {
	
	//randomly generate initial population
	public List<Route> randGenePop(){
		//1. reshuffle cities order 
		List<SortObject> soList = new ArrayList();
		for(int i=1;i<GlobalInformation.cityNum;i++){
			SortObject so = new SortObject();
			so.setPosition(i);
			so.setValue(Math.random());
			soList.add(so);
		}
		Collections.sort(soList,new MyComparator());
		
		//2. according to this order, generate a feasible solution
		List<Route> routeList = new ArrayList<Route>();				
		routeList.add(new Route());
		
		for(int i=0;i<soList.size();i++){
			Route currentRoute = routeList.get(routeList.size()-1);
			//get current city
			City city = GlobalInformation.cityList.get(soList.get(i).getPosition()).clone();
			//insert this city into current route
			//firstly check demand and time feasibility
			if(currentRoute.checkDemandFeasibility(city)&&currentRoute.checkTimeFeasibility(city, currentRoute.getLastCityIndex())){
				currentRoute.insertCityIntoRoute(city, currentRoute.getLastCityIndex());
			}else{
				//add this route into current route list and generate a new route
				Route newRoute = new Route();
				newRoute.insertCityIntoRoute(city, newRoute.getLastCityIndex());
				routeList.add(newRoute);				
			}
		}
		
		return routeList;
	}
	
	
	
}
